package cn.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Druid配置类
 */
@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DruidDataSource druidDataSource() {
        return new DruidDataSource();
    }

    /*
    * 配置监控服务器
    *
    * */

    /**
     * 配置监控服务器
     * @return 返回监控注册的servlet对象
     */
    @Bean
    public ServletRegistrationBean statViewServlet() {
        ServletRegistrationBean registry = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        // 添加白名单
        registry.addInitParameter("all", "");
        // 添加黑名单
        registry.addInitParameter("deny", "192.168.31.1");
        // 添加控制台管理用户
        registry.addInitParameter("loginUsername", "sqlAdmin");
        registry.addInitParameter("loginPassword", "sqlRoot");

        // 是否可以充值数据
        registry.addInitParameter("resetEnable", "false");
        System.out.println("Druid Init");

        return registry;
    }


    /**
     * 配置服务过滤起
     * @return 返回过滤器配置对象
     */
    @Bean
    public FilterRegistrationBean statFilter() {
        FilterRegistrationBean filter = new FilterRegistrationBean(new WebStatFilter());
        // 添加过滤规则
        filter.addUrlPatterns("/*");
        // 忽略过滤格式
        filter.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");


        return filter;

    }











}
